<%@ page import="cn.school.entity.Student" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@ page import="cn.school.service.StudentService" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: ghost
  Date: 2019/12/8
  Time: 22:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%@include file="checkLogin.jsp"%>
<%!
    public Date setDate(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
%>
<%
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(1024*1024);
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding("utf-8");
    upload.setSizeMax(1024*1024);
    List<FileItem> list = upload.parseRequest(request);
    Student student = new Student();
    int id  = 0;
    for(FileItem f:list){
        if(f.isFormField()){    //判断是否为表达元素
            if(f.getFieldName().equals("id")){
                id = Integer.parseInt(f.getString("utf-8"));
                student.setStuno(id);
            }else if(f.getFieldName().equals("sname")){
                student.setSname(f.getString("utf-8"));
            }else if(f.getFieldName().equals("oldidcardpic")){
                student.setIdcardpic(f.getString("utf-8"));
            }else if(f.getFieldName().equals("sex")){
                student.setSex(f.getString("utf-8"));
            }else if(f.getFieldName().equals("telephone")){
                student.setTelephone(f.getString("utf-8"));
            }else if(f.getFieldName().equals("qq")){
                student.setQq(f.getString("utf-8"));
            }else if(f.getFieldName().equals("wechat")){
                student.setWechat(f.getString("utf-8"));
            }else if(f.getFieldName().equals("gradeid")){
                student.setGradeid(Long.parseLong(f.getString("utf-8")));
            }else if(f.getFieldName().equals("fromcity")){
                student.setFromcity(f.getString("utf-8"));
            }else if(f.getFieldName().equals("birthday")){
                student.setBirthday(setDate(f.getString("utf-8")));
            }else if(f.getFieldName().equals("address")){
                student.setAddress(f.getString("utf-8"));
            }else if(f.getFieldName().equals("pass")){
                student.setPass(f.getString("utf-8"));
            }
        }else{
            if(f.getSize()==0){
                continue;
            }
            File path = new File(application.getRealPath("img"));
            if(path.exists() == false){
                path.mkdirs();
            }
//            String suffix = f.getName().substring(f.getName().lastIndexOf("."));
            String newName = student.getIdcardpic();
            f.write(new File(path,newName));
        }
    }
    StudentService ss = new StudentService();
    if(ss.update(student) == 1){
        out.print("<script>alert('更新成功！');" +
                "window.location.href='studentinfo.jsp';</script>");
    }else{
        out.print("<script>alert('更新失败！');" +
                "window.location.href='alterStudent.jsp?id="+id+"';</script>");
    }
%>
</body>
</html>